/*
 * Copyright 2019 The ZuSmart Project
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zusmart.base.logging.support;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.zusmart.base.logging.format.Formatter;
import com.zusmart.base.logging.format.FormatterTuple;

/**
 * @author Administrator
 *
 */
public class Log4JLogger extends AbstractLogger {

	private static final long serialVersionUID = 3235942005472882828L;
	private static final String FQCN = Log4JLogger.class.getName();

	private final transient Logger logger;
	private final boolean traceCapable;

	public Log4JLogger(Logger logger) {
		super(logger.getName());
		this.logger = logger;
		this.traceCapable = this.isTraceCapable();
	}

	private boolean isTraceCapable() {
		try {
			this.logger.isTraceEnabled();
			return true;
		} catch (NoSuchMethodError e) {
			return false;
		}
	}

	@Override
	public boolean isTraceEnabled() {
		if (this.traceCapable) {
			return this.logger.isTraceEnabled();
		} else {
			return this.logger.isDebugEnabled();
		}
	}

	@Override
	public boolean isDebugEnabled() {
		return this.logger.isDebugEnabled();
	}

	@Override
	public boolean isInfoEnabled() {
		return this.logger.isInfoEnabled();
	}

	@Override
	public boolean isWarnEnabled() {
		return this.logger.isEnabledFor(Level.WARN);
	}

	@Override
	public boolean isErrorEnabled() {
		return this.logger.isEnabledFor(Level.ERROR);
	}

	@Override
	public void trace(String msg) {
		if (this.isTraceEnabled()) {
			this.logger.log(FQCN, this.traceCapable ? Level.TRACE : Level.DEBUG, msg, null);
		}
	}

	@Override
	public void trace(String msg, Object arg) {
		if (this.isTraceEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, arg);
			this.logger.log(FQCN, this.traceCapable ? Level.TRACE : Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void trace(String msg, Object argA, Object argB) {
		if (this.isTraceEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, argA, argB);
			this.logger.log(FQCN, this.traceCapable ? Level.TRACE : Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void trace(String msg, Object... args) {
		if (this.isTraceEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, args);
			this.logger.log(FQCN, this.traceCapable ? Level.TRACE : Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void trace(String msg, Throwable cause) {
		if (this.isTraceEnabled()) {
			this.logger.log(FQCN, this.traceCapable ? Level.TRACE : Level.DEBUG, msg, cause);
		}
	}

	@Override
	public void debug(String msg) {
		if (this.isDebugEnabled()) {
			this.logger.log(FQCN, Level.DEBUG, msg, null);
		}
	}

	@Override
	public void debug(String msg, Object arg) {
		if (this.isDebugEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, arg);
			this.logger.log(FQCN, Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void debug(String msg, Object argA, Object argB) {
		if (this.isDebugEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, argA, argB);
			this.logger.log(FQCN, Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void debug(String msg, Object... args) {
		if (this.isDebugEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, args);
			this.logger.log(FQCN, Level.DEBUG, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void debug(String msg, Throwable cause) {
		if (this.isDebugEnabled()) {
			this.logger.log(FQCN, Level.DEBUG, msg, cause);
		}
	}

	@Override
	public void info(String msg) {
		if (this.isInfoEnabled()) {
			this.logger.log(FQCN, Level.INFO, msg, null);
		}
	}

	@Override
	public void info(String msg, Object arg) {
		if (this.isInfoEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, arg);
			this.logger.log(FQCN, Level.INFO, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void info(String msg, Object argA, Object argB) {
		if (this.isInfoEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, argA, argB);
			this.logger.log(FQCN, Level.INFO, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void info(String msg, Object... args) {
		if (this.isInfoEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, args);
			this.logger.log(FQCN, Level.INFO, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void info(String msg, Throwable cause) {
		if (this.isInfoEnabled()) {
			this.logger.log(FQCN, Level.INFO, msg, cause);
		}
	}

	@Override
	public void warn(String msg) {
		if (this.isWarnEnabled()) {
			this.logger.log(FQCN, Level.WARN, msg, null);
		}
	}

	@Override
	public void warn(String msg, Object arg) {
		if (this.isWarnEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, arg);
			this.logger.log(FQCN, Level.WARN, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void warn(String msg, Object argA, Object argB) {
		if (this.isWarnEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, argA, argB);
			this.logger.log(FQCN, Level.WARN, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void warn(String msg, Object... args) {
		if (this.isWarnEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, args);
			this.logger.log(FQCN, Level.WARN, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void warn(String msg, Throwable cause) {
		if (this.isWarnEnabled()) {
			this.logger.log(FQCN, Level.WARN, msg, cause);
		}
	}

	@Override
	public void error(String msg) {
		if (this.isErrorEnabled()) {
			this.logger.log(FQCN, Level.ERROR, msg, null);
		}
	}

	@Override
	public void error(String msg, Object arg) {
		if (this.isErrorEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, arg);
			this.logger.log(FQCN, Level.ERROR, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void error(String msg, Object argA, Object argB) {
		if (this.isErrorEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, argA, argB);
			this.logger.log(FQCN, Level.ERROR, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void error(String msg, Object... args) {
		if (this.isErrorEnabled()) {
			FormatterTuple tuple = Formatter.format(msg, args);
			this.logger.log(FQCN, Level.ERROR, tuple.getMessage(), tuple.getThrowable());
		}
	}

	@Override
	public void error(String msg, Throwable cause) {
		if (this.isErrorEnabled()) {
			this.logger.log(FQCN, Level.ERROR, msg, cause);
		}
	}

}